import tkinter as tk
from tkinter import filedialog

import config
from controllers.swagger2word_controller import Swagger2WordController


class Swagger2WordWindow(tk.Frame):
    def __init__(self, master, main_window):
        super().__init__(master)
        self.entry_frame = None
        self.status_label = None
        self.submit_button = None
        self.file_button = None
        self.entry = None
        self.var = None
        self.main_window = main_window
        self.create_widgets()

    def create_widgets(self):
        # 页面标签
        self.page_label = tk.Label(self, text="swagger转word文档")
        self.page_label.pack(anchor="w", padx=10, pady=10)

        # 单选框变量
        self.var = tk.StringVar()

        # 单选框
        radio_frame = tk.Frame(self)
        radio_a = tk.Radiobutton(radio_frame, text="接口地址", variable=self.var, value='a', command=self.on_select)
        radio_b = tk.Radiobutton(radio_frame, text="本地文件", variable=self.var, value='b', command=self.on_select)
        # radio_a.pack(anchor=tk.W)
        # radio_b.pack(anchor=tk.W)
        radio_a.grid(row=0, column=0)
        radio_b.grid(row=0, column=1)
        radio_frame.pack(fill='x', padx=10, pady=10)

        # 输入框和文件选择按钮的框架
        self.entry_frame = tk.Frame(self)
        self.entry = tk.Entry(self.entry_frame, width=config.ENTRY_WIDTH)  # 调整输入框的宽度
        self.entry.grid(row=0, column=0)

        # 文件选择按钮（仅在选择了b时有效）
        self.file_button = tk.Button(self.entry_frame, text="浏览", command=self.browse_file)
        self.file_button.grid_forget()  # 初始隐藏文件选择按钮

        # 将输入框和文件选择按钮放在同一个框架中
        self.entry_frame.pack(fill='x', padx=10, pady=10)

        # 提交按钮
        self.submit_button = tk.Button(self, text="开始", command=self.submit)
        self.submit_button.pack(anchor="w", padx=10, pady=10, ipadx=50)

        # 状态标签
        self.status_label = tk.Label(self, text="")
        self.status_label.pack(pady=5)

        # 初始化单选框状态
        self.var.set('a')
        self.on_select()

    def on_select(self):
        # 根据选择更新输入框的提示信息
        if self.var.get() == 'a':
            self.entry.config(state=tk.NORMAL)
            self.entry.delete(0, tk.END)
            # self.entry.insert(0, "请输入接口地址")
            self.file_button.grid_forget()  # 隐藏文件选择按钮
            self.entry_frame.pack(pady=5)  # 显示输入框和按钮
        else:
            self.entry.config(state=tk.NORMAL)
            self.entry.delete(0, tk.END)
            # self.entry.insert(0, "请点击浏览选择文件")
            self.file_button.grid(row=0, column=1)  # 显示文件选择按钮
            self.entry_frame.pack(pady=5)  # 显示输入框和按钮

    def browse_file(self):
        # 打开文件选择对话框
        filename = filedialog.askopenfilename()
        self.entry.delete(0, tk.END)
        self.entry.insert(0, filename)

    def submit(self):
        # 获取输入框中的值并打印
        input_file_path = self.entry.get()
        print("您输入的地址或文件路径为:", input_file_path)
        self.status_label.config(text="生成中...")
        self.submit_button.config(state=tk.DISABLED)
        self.master.update_idletasks()

        try:
            swagger2word_controller = Swagger2WordController(self.master)
            output_word_path = swagger2word_controller.swagger2word(input_file_path)
            print(f"文档已保存至: {output_word_path}")
            self.status_label.config(text=f"已成功生成：{output_word_path}")
        except Exception as e:
            print(e)
            self.status_label.config(text=f"生成失败：{e}")
        self.submit_button.config(state=tk.NORMAL)